/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.core.resources;

import java.io.InputStream;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.api.ILogEntry;
import net.sourceforge.p4eclipse.core.api.PerforceUtil;
import net.sourceforge.p4eclipse.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class RemoteFile extends RemoteResource
{
	private String depotFileName;
	private String revision;
	private ResourceSyncInfo resourceSyncInfo;
	
	public RemoteFile(String depotFileName, String revision)
	{
		this.depotFileName = depotFileName;
		this.revision = revision;
	}
	
	/*
	 * @see IRemoteResource#getContents(IProgressMonitor)
	 */
	public InputStream getContents(IProgressMonitor progress) 
		throws TeamException
	{
		return PerforceUtil.print(depotFileName, revision);
	}


	/*
	 * @see IRemoteResource#getName()
	 */
	public String getName()
	{
		int start = depotFileName.lastIndexOf('/');
		return depotFileName.substring(start + 1);
	}

	/*
	 * @see IRemoteResource#isContainer()
	 */
	public boolean isContainer()
	{
		return false;
	}

	/*
	 * @see RemoteResource#getSyncInfo()
	 */
	public ResourceSyncInfo getSyncInfo()
	{
		try
		{
			if (resourceSyncInfo == null)
				resourceSyncInfo = PerforceUIPlugin.getClient().fstat(depotFileName + revision)[0];
				
			return resourceSyncInfo;
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus());
		}
		
		return null;
	}

	/*
	 * @see IRemoteResource#members(IProgressMonitor)
	 */
	public IRemoteResource[] members(IProgressMonitor progress)
		throws TeamException
	{
		return new IRemoteResource[0];
	}
	
	public String getDepotFilename()
	{
		return getSyncInfo().getDepotFileName();
	}

	/** @see IP4RemoteFile#getLogEntries(IProgressMonitor) */
	public ILogEntry[] getLogEntries(boolean followBranches, IProgressMonitor monitor)
		throws TeamException
	{	
		return PerforceUIPlugin.getClient().filelog(this, 25, followBranches);
	}
}
